<?php
/**
 * DMFramework
 * 
 * @license Copyright 2012 Dillen Meijboom under the Apache license version 2.0 (http://apache.org/licenses/LICENSE-2.0)
 * @author Dillen Meijboom <info@dillenm.nl>
 * @url http://dmframework.nl
 */

/**
 * The resource class can open a file and add it to the main output.
 * 
 * @license Copyright 2012 Dillen Meijboom under the Apache license version 2.0 (http://apache.org/licenses/LICENSE-2.0)
 * @author Dillen Meijboom <info@dillenm.nl>
 * @package System.Core
 */
class Dmf_Resource
{
    /**
     * Which file to use
     * 
     * @var string
     */
    protected $_filename;
    
    /**
     * Replaces
     * 
     * @var array 
     */
    protected $_replaces = array();
    
    /**
     * All variable memory for the resource
     * 
     * @var array
     */
    protected $_memory = array();
    
    /**
     * The parser
     * 
     * @var Dmf_Parser
     */
    protected $_parser;
    
    /**
     * Configuration settings
     *  
     * @var Object
     */
    protected $_config;
    
    /**
     * Constructor
     * 
     * @param string $filename 
     */
    public function __construct( $filename )
    {
        $this->_filename = $filename;
        $this->_config = Dmf_Application::getInstance()->getConfig()->asObject();
    }
    
    /**
     * Get a variable
     * 
     * @param string $index
     * @return mixed
     */
    public function __get( $index )
    {
        return isset( $this->_memory[ $index ] ) ? $this->_memory[ $index ] : null;
    }
    
    /**
     * Assign a variable to the view
     * 
     * @param string $index
     * @param mixed $value 
     */
    public function assign( $index, $value )
    {
        $this->_memory[ $index ] = $value;
    }
    
    /**
     * Import memory to a certain name
     *  
     * @param array $memory
     * @param string $name
     */
    public function import( $memory, $name )
    {
        $this->_memory[ $name ] = Dmf_Stdclass::fromArray( $memory );
    }
    
    /**
     * Set the filename
     * 
     * @param string $file 
     */
    public function setFile( $file )
    {
        $this->_filename = $file;
    }
    
    /**
     * Get filename
     * 
     * @return string 
     */
    public function getFile()
    {
        return $this->_filename;
    }
    
    /**
     * Replace content
     *  
     * @param string $from
     * @param string $to 
     */
    public function replace( $from, $to )
    {
        $this->_replaces[] = array( $from, $to );
    }
    
    /**
     * Include the file
     * 
     * @param string $file
     */
    private function _include( $file )
    {
        if ( count( $this->_replaces ) > 0 )
        {
            ob_start();
        }
        
        include( $file );
        
        if ( count( $this->_replaces ) > 0 )
        {
            $content = @ob_get_clean();
            
            foreach( $this->_replaces as $replace )
            {
                if ( strstr( '/', $replace['0'] ) )
                {
                    $content = preg_replace( $replace['0'], $replace['1'], $content );
                }
                else
                {
                    $content = str_replace( $replace['0'], $replace['1'], $content );
                }
            }
            
            echo $content;
        }
    }
    
    /**
     * Get all resource memory
     *   
     * @var array
     */
    public function getMemory()
    {
        return $this->_memory;
    }
    
    /**
     * Set resource memory
     * 
     * @param array $memory 
     */
    public function setMemory( $memory )
    {
        $this->_memory = $memory;
    }
    
    /**
     * Render the resource and add to main output buffer.
     * 
     * @return null
     * @throws Dmf_Exception_FileNotFound 
     */
    public function render()
    {
        if ( ! file_exists( $this->_filename ) )
        {
            throw new Dmf_Exception_FileNotFound( 'Resource file: "' . $this->_filename . '" not found.' );
        }
        
        if ( ! $this instanceof Dmf_Layout )
        {
            if ( ! $this->_config->view->parser )
            {
                $this->_include( $this->getFile(), $this->getFile() );
                return;
            }

            $this->_parser = new Dmf_Parser( $this->getFile() );

            $this->_include( $this->_parser->getFile(), $this->getFile() );
            return;
        }
        
        if ( $this->_config->view->parser )
        {
            $this->_parser = new Dmf_Parser( $this->_filename );
        }
        
        $this->_include( ( isset( $this->_parser ) ? $this->_parser->getFile() : $this->_filename ), $this->getFile() );
    }
    
    /**
     * Delete the parser-tmp-file 
     */
    public function __destruct()
    {
        if ( $this->_config->view->parser && $this->_parser instanceof Dmf_Parser )
        {
            $this->_parser->removeTempFile();
        }
    }
}